package com.yt.app.api.action.pay;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Splitter;
import com.yt.app.api.action.base.BaseAction;
import com.yt.app.api.action.pay.util.AlipayUtils;
import com.yt.app.api.action.pay.util.WechatPayUtils;
import com.yt.app.core.interfaces.UserOrderDetailService;
import com.yt.app.core.interfaces.UserOrderPayLogService;
import com.yt.app.core.interfaces.UserOrderService;
import com.yt.app.core.model.UserOrder;
import com.yt.app.core.model.UserOrderDetail;
import com.yt.app.core.utils.Constant;
import com.yt.core.MsgBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * Description
 * <p>
 *     支付宝支付接口
 * </p>
 * DATE 2017/10/8.
 *
 * @author liujunshi.
 */

@Controller
@Api(tags = "11.支付")
@RequestMapping("/pay")
public class PayController extends BaseAction{
    @Autowired
    private UserOrderService userOrderService;

    @Autowired
    private UserOrderDetailService userOrderDetailService;

    @Autowired
    private UserOrderPayLogService payLogService;

    @ApiOperation(value="开始支付", notes = "获取APP支付需要的订单字符串", httpMethod = "POST")
    @RequestMapping("/getPayParams")
    public @ResponseBody
    Object toPay(
            @ApiParam("订单号") @RequestParam(value = "orderNo", required = true, defaultValue = "0") String orderNo,
            @ApiParam("支付方式：1：微信,2：支付宝") @RequestParam(value = "payType", required = true, defaultValue = "0") Integer payType,
            @ApiIgnore HttpServletRequest request,
            @ApiIgnore HttpServletResponse response
    ){
        MsgBean msgBean = new MsgBean();
        UserOrder userOrder = userOrderService.getByOrderNo(orderNo);
        if(userOrder == null){
            return msgBean.failure("订单不存在!").returnMsg();
        }
        if(!userOrder.getOrderStatus().equals(Constant.ORDER_STATUS.NOT_PAY.value())){
            return msgBean.failure("订单状态错误，只支持为支付的订单").returnMsg();
        }
        //更新付款方式
        userOrder.setPayType(payType + "");
        userOrderService.update(userOrder);
        List<UserOrderDetail> userOrderDetailList = userOrderDetailService.getByOrderId(userOrder.getId());
        StringBuilder body = new StringBuilder();
        body.append(userOrder.getBuyUser()).append(":");
        for(UserOrderDetail detail : userOrderDetailList){
            body.append(detail.getProductName()).append(",");
        }

        if(body.length() > 120){
            body.delete(120, body.length()-1);
        }
        body.append("...");

        if(payType == Constant.PAY_TYPE.ALIPAY.value()){
            Map param = AlipayUtils.getPayParams(userOrder.getOrderNo(), body.toString(), userOrder.getAmount() + "");
            msgBean.put("parameterMap", param);
        } else if(payType == Constant.PAY_TYPE.WECHAT.value()){
            Map param = WechatPayUtils.getPayParams(userOrder.getOrderNo(), body.toString(), userOrder.getAmount() + "", request.getRemoteAddr());
            msgBean.put("parameterMap", param);
        }
        return msgBean.returnMsg();
    }
}
